﻿Imports System.Windows.Forms
Imports Way.Windows.Toolkit
Imports ControleMatricula.Negocio



Module mGlobal
    Friend GlobalLogin As Integer = 0
    Friend GlobalVersao As String = "Sistema Específica" & " 2010 (v." & My.Application.Info.Version.Major.ToString & My.Application.Info.Version.Minor.ToString.PadLeft(2, "0") & My.Application.Info.Version.Build.ToString & My.Application.Info.Version.Revision.ToString & ")"
    Friend GlobalVersaoMDI As String = GlobalVersao & " - TI Consultoria em Informática - suporte@ticonsultoria.com.br - (61) 9249-9419 - www.ticonsultoria.com.br"
    Dim db As New DataClassesDataContext()


    Sub LimparCampos(ByRef Ctrl As Object)
        If (TypeOf (Ctrl) Is Form Or TypeOf (Ctrl) Is GroupBox Or TypeOf (Ctrl) Is Panel Or TypeOf (Ctrl) Is TabControl Or TypeOf (Ctrl) Is Way.Windows.Toolkit.WayForm Or TypeOf (Ctrl) Is Way.Windows.Navigator.WayPage Or TypeOf (Ctrl) Is Way.Windows.Navigator.WayNavigator) Then
            For Each _ctrl As Object In Ctrl.Controls
                LimparCampos(_ctrl)
            Next
        ElseIf (TypeOf (Ctrl) Is CheckBox Or TypeOf (Ctrl) Is CheckBox Or TypeOf (Ctrl) Is RadioButton Or TypeOf (Ctrl) Is RadioButton Or TypeOf (Ctrl) Is Way.Windows.Toolkit.WayCheckBox) Then
            Ctrl.Checked = False
        ElseIf (TypeOf (Ctrl) Is ComboBox Or TypeOf (Ctrl) Is ComboBox Or TypeOf (Ctrl) Is ListBox Or TypeOf (Ctrl) Is ListBox Or TypeOf (Ctrl) Is Way.Windows.Toolkit.WayComboBox) Then
            Ctrl.SelectedIndex = -1
        ElseIf (TypeOf (Ctrl) Is CheckedListBox) Then
            For i As Short = (Ctrl.CheckedIndices.Count - 1) To 0 Step -1
                Ctrl.SetItemChecked(Ctrl.CheckedIndices(i), False)
            Next
        ElseIf (TypeOf (Ctrl) Is MaskedTextBox Or TypeOf (Ctrl) Is RichTextBox Or TypeOf (Ctrl) Is TextBox Or TypeOf (Ctrl) Is Way.Windows.Toolkit.WayTextBox) Then
            Ctrl.Text = ""
        ElseIf (TypeOf (Ctrl) Is DateTimePicker) Then
            Ctrl.Value = Date.Now.Date
        ElseIf (TypeOf (Ctrl) Is NumericUpDown) Then
            Ctrl.Text = "0"
        End If
    End Sub

    Function ValidarCNPJ(ByRef CNPJ As String) As Short

        Dim FUValidarCNPJ As Object
        Dim d1, j, retorno, a, i, d2 As Object

        Try

            If Len(CNPJ) = 8 And Val(CNPJ) > 0 Then
                a = 0
                j = 0
                d1 = 0

                For i = 1 To 7
                    a = Val(Mid(CNPJ, i, 1))
                    If (i Mod 2) <> 0 Then
                        a = a * 2
                    End If

                    If a > 9 Then
                        j = j + Int(a / 10) + (a Mod 10)
                    Else
                        j = j + a
                    End If

                Next i
                d1 = IIf((j Mod 10) <> 0, 10 - (j Mod 10), 0)

                If d1 = Val(Mid(CNPJ, 8, 1)) Then
                    FUValidarCNPJ = True
                Else
                    FUValidarCNPJ = False
                End If

            Else

                If Len(CNPJ) = 14 And Val(CNPJ) > 0 Then
                    a = 0
                    i = 0
                    d1 = 0
                    d2 = 0
                    j = 5

                    For i = 1 To 12 Step 1
                        a = a + (Val(Mid(CNPJ, i, 1)) * j)
                        j = IIf(j > 2, j - 1, 9)
                    Next i

                    a = a Mod 11
                    d1 = IIf(a > 1, 11 - a, 0)
                    a = 0
                    i = 0
                    j = 6

                    For i = 1 To 13 Step 1
                        a = a + (Val(Mid(CNPJ, i, 1)) * j)
                        j = IIf(j > 2, j - 1, 9)
                    Next i

                    a = a Mod 11
                    d2 = IIf(a > 1, 11 - a, 0)

                    If (d1 = Val(Mid(CNPJ, 13, 1)) And d2 = Val(Mid(CNPJ, 14, 1))) Then
                        ValidarCNPJ = True
                    Else
                        ValidarCNPJ = False
                    End If

                Else
                    ValidarCNPJ = False
                End If
            End If

        Catch Er As Exception

        End Try

    End Function

    Function GetAssinatura(ByVal IDEspecifica As Integer) As DataSet
        Dim oObj As New DataSet

        Try

            Dim ListAssinatura As List(Of SPAssinaturaConsultarResult)
            ListAssinatura = db.SPAssinaturaConsultar(IDEspecifica).ToList()

            Dim DSAssinatura As DataSet
            DSAssinatura = New DataSet()

            DSAssinatura.Tables.Add(New DataTable("SPAssinatura"))

            DSAssinatura.Tables(0).Columns.Add("Assinatura")

            Dim row As DataRow = DSAssinatura.Tables(0).NewRow()

            row("Assinatura") = ListAssinatura(0).Assinatura.ToArray()

            DSAssinatura.Tables(0).Rows.Add(row)
            DSAssinatura.AcceptChanges()


            oObj = (DSAssinatura)
            GetAssinatura = oObj

        Catch Err As Exception
            MsgBox(Err.ToString, MsgBoxStyle.Critical, "Específica")
        Finally
            DisposeObjeto(oObj)
            oObj = Nothing
        End Try

    End Function

    Sub DisposeObjeto(ByRef Obj As Object)
        Try
            'Libera um objeto da memoria, se der erro nao faz nada!
            Obj.Dispose()
        Catch
        End Try
    End Sub

    Function Numeric(ByVal Keyascii As Short) As Short

        If InStr("1234567890", Chr(Keyascii)) = 0 Then
            Numeric = 0
        Else
            Numeric = Keyascii
        End If
        Select Case Keyascii
            Case 8
                Numeric = Keyascii
            Case 13
                Numeric = Keyascii
            Case 32
                Numeric = Keyascii
        End Select

    End Function


    Function MontarComboEspecifica(ByRef cmbEspecifica As Control, Optional ByVal bTodas As Boolean = True)

        Dim RetornoEspecifica As New List(Of SPEspecificaConsultarResult)
        RetornoEspecifica = db.SPEspecificaConsultar(0, "").Where(Function(p) p.STAtivo = True).ToList()

        If (TypeOf (cmbEspecifica) Is WayComboBox) Then
            CType(cmbEspecifica, WayComboBox).DataSource = RetornoEspecifica
            CType(cmbEspecifica, WayComboBox).DisplayMember = "DEEspecifica"
            CType(cmbEspecifica, WayComboBox).ValueMember = "IDEspecifica"
        Else
            CType(cmbEspecifica, ComboBox).DataSource = RetornoEspecifica
            CType(cmbEspecifica, ComboBox).DisplayMember = "DEEspecifica"
            CType(cmbEspecifica, ComboBox).ValueMember = "IDEspecifica"
        End If

    End Function

    Function MontarComboPagamento(ByRef cmbPagamento As Control, Optional ByVal bTodas As Boolean = True)

        Dim RetornoPagamento As New List(Of SPPagamentoConsultarResult)
        RetornoPagamento = db.SPPagamentoConsultar(0, "").Where(Function(p) p.STAtivo = True).ToList()

        If (TypeOf (cmbPagamento) Is WayComboBox) Then
            CType(cmbPagamento, WayComboBox).DataSource = RetornoPagamento
            CType(cmbPagamento, WayComboBox).DisplayMember = "DEPagamento"
            CType(cmbPagamento, WayComboBox).ValueMember = "IDPagamento"
        Else
            CType(cmbPagamento, ComboBox).DataSource = RetornoPagamento
            CType(cmbPagamento, ComboBox).DisplayMember = "DEPagamento"
            CType(cmbPagamento, ComboBox).ValueMember = "IDPagamento"
        End If


    End Function

    'Function UsuarioLogado(ByRef Login As String)

    '    Dim Usuario As String
    '    Dim IDUsuario As Integer
    '    Dim RetornoUsuario As New List(Of SPFuncionarioConsultarResult)
    '    RetornoUsuario = db.SPFuncionarioConsultar(0, Login)

    '    Usuario = RetornoUsuario(0).Login
    '    IDUsuario = RetornoUsuario(0).IDFuncionario


    'End Function

    Function ValidarCPF(ByRef CPF As String) As Short
        Dim soma As Short
        Dim Resto As Short
        Dim i As Short

        Try
            If Len(CPF) <> 11 Then
                ValidarCPF = False
                Exit Function
            End If

            soma = 0

            For i = 1 To 9
                soma = soma + Val(Mid(CPF, i, 1)) * (11 - i)
            Next i

            Resto = 11 - (soma - (Int(soma / 11) * 11))

            If Resto = 10 Or Resto = 11 Then Resto = 0

            If Resto <> Val(Mid(CPF, 10, 1)) Then
                ValidarCPF = False
                Exit Function
            End If

            soma = 0

            For i = 1 To 10
                soma = soma + Val(Mid(CPF, i, 1)) * (12 - i)
            Next i

            Resto = 11 - (soma - (Int(soma / 11) * 11))

            If Resto = 10 Or Resto = 11 Then Resto = 0
            If Resto <> Val(Mid(CPF, 11, 1)) Then
                ValidarCPF = False
                Exit Function
            End If

            ValidarCPF = True

        Catch Er As Exception
            MsgBox(Er.Message, MsgBoxStyle.Critical, "Gestor.Net")
        End Try

    End Function

End Module


